//
//  HiChangeNameController.m
//  HiCar
//
//  Created by 丁一 on 2019/1/11.
//  Copyright © 2019 丁一. All rights reserved.
//

#import "HiChangeNameController.h"
#import "HiJonesClass.h"
#import "UserModel.h"
#import "SensitiveWordTools.h"

@interface HiChangeNameController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *Textfiled;
@property (nonatomic, strong) UIButton *buttondone;

@end

@implementation HiChangeNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = _Type==0?@"添加昵称":@"修改昵称";
    
    self.view.backgroundColor=LineColor;
    [self.view addSubview:self.Textfiled];
    
    self.buttondone = [[UIButton alloc] initWithFrame:CGRectMake(20, NAV_Height +75, RECRWITCH-40, 43)];
    self.buttondone.layer.cornerRadius  = 6;
    self.buttondone.layer.masksToBounds = YES;
    [self.buttondone.titleLabel setFont:[UIFont boldSystemFontOfSize:17]];
    [self.buttondone addTarget:self action:@selector(addname) forControlEvents:UIControlEventTouchUpInside];
    [self.buttondone setBackgroundImage:[JonesClass createImageWithColor:Buttoncolor] forState:UIControlStateNormal];
    [self.buttondone setTitle:@"完  成" forState:UIControlStateNormal];
    [self.view addSubview:self.buttondone];
}

- (void)addname{

    NSString *linstrong = _Textfiled.text;
    
    BOOL hasSensitive = [[SensitiveWordTools sharedInstance] hasSensitiveWord:linstrong];
    
    if (hasSensitive) {
        NSString *string = [NSString stringWithFormat:@"含有敏感词汇：%@",[[SensitiveWordTools sharedInstance]filter:linstrong]];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提醒" message:string preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:sureAction];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
    }else{
        _Textfiled.text = [linstrong stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        if ([HiJonesClass checkName:self.Textfiled.text]) {
            _usermodle.name = self.Textfiled.text;
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [SVProgressHUD showErrorWithStatus:@"昵称在1-12位"];
        }
    }
}

- (UITextField *)Textfiled{
    if (!_Textfiled) {
        _Textfiled    = [[UITextField alloc] initWithFrame:CGRectMake(20, NAV_Height +10, RECRWITCH-40, 46)];
        _Textfiled.textColor       = [UIColor blackColor];
        _Textfiled.font            = [UIFont boldSystemFontOfSize:20];
        _Textfiled.backgroundColor = [UIColor whiteColor];
        _Textfiled.delegate        = self;
        _Textfiled.returnKeyType = UIReturnKeyDone;
        _Textfiled.layer.borderWidth = 0;
        _Textfiled.layer.cornerRadius=10;
        _Textfiled.placeholder=_Type==0?@"添加昵称":@"修改昵称";
        _Textfiled.text = _usermodle.name;
        _Textfiled.textAlignment=NSTextAlignmentCenter;
        [_Textfiled addTarget:self action:@selector(passConTextChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _Textfiled;
}

-(void)passConTextChange:(id)sender{
    UITextField* target   = (UITextField*)sender;
    if (target.text.length>0) {
        self.buttondone.backgroundColor=[UIColor orangeColor];
    }else{
        self.buttondone.backgroundColor=[UIColor grayColor];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    BOOL flag = NO;
    [textField resignFirstResponder];
    return flag;
}



- (void)textFieldDidBeginEditing:(UITextField *)textField{

}


- (void)textFieldDidEndEditing:(UITextField *)textField{

}

@end
